﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SWS;

public class AI_CarController : MonoBehaviour
{

    public splineMove moveConponent;
    public List<PathManager> currentPathStep;
    public int currentPathIndex;

    private void Awake()
    {
        moveConponent = GetComponent<splineMove>();
    }

    /// <summary>
    /// 制定AI车辆行驶路线，并添加结束事件然后开始
    /// </summary>
    public void SetCarMoveStep(List<PathManager> pathList)
    {
        currentPathStep = pathList;
        currentPathIndex = pathList.Count - 1;
        //添加结束事件
        AddOverAction(currentPathStep[currentPathIndex], moveConponent);
        moveConponent.StartMove();
        print("开始第"+ currentPathIndex+"条路径");
    }

    /// <summary>
    /// 判断是否已经走到终点，到了结束导航，没到给下一个路线继续行驶，并在结束事件中继续判断此事
    /// </summary>
    public void OnPathCompleteAction()
    {
        print("第" + currentPathIndex + "条路径结束");
        moveConponent.events.Clear();
        currentPathIndex --;
        if (currentPathIndex < 0)
        {
            //todo 已经走到终点

            print("到终点，导航结束");
        }
        else
        {
            //添加结束事件
            AddOverAction(currentPathStep[currentPathIndex], moveConponent);
        }
    }

    public void AddOverAction(PathManager path,splineMove aiController)
    {
        aiController.SetPath(path);
        aiController.events[path as BezierPathManager ? (moveConponent.events.Count - 1) : path.waypoints.Length - 1].AddListener(OnPathCompleteAction);
    }
}
